package com.gmlive.common.ui.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;

import androidx.annotation.ColorInt;

import com.gmlive.common.ui.R;

/**
 * @Author: 姜志敏
 * @CreateDate: 2019-07-11 16:34
 */
public class ResHelper {

    // 圆角按钮边线宽度dp值
    private final static int DRAWABLE_STROKE_WIDTH = 1;

    // 按钮默认不可点击颜色
    private final static int DRAWABLE_DISABLE_COLOR = 0xffececec;

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据原色生成按下状态颜色
     * 叠加20%不透明黑色
     */
    private static int createPressedColor(int color) {
        return createNewColor(color, 0x000000, 0.2f);
    }

    private static int createNewColor(int color, int overlayColor, float alpha) {
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);

        int ovR = Color.red(overlayColor);
        int ovG = Color.green(overlayColor);
        int ovB = Color.blue(overlayColor);

        int newR = createColor(r, ovR, alpha);
        int newG = createColor(g, ovG, alpha);
        int newB = createColor(b, ovB, alpha);

        return Color.rgb(newR, newG, newB);
    }

    private static int createColor(int color, int overlay, float alpha) {
        return (int) (color * (1 - alpha) + overlay * alpha);
    }

    public static int getThemeColor(Context context) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        return typedValue.data;
    }

    /**
     * 创建圆角按钮的背景，包含三种状态：normal, disable, pressed
     * @param color     主色调
     * @param radius    圆角半径
     * @param solid     是否填充，如果非填充则背景有边线
     */
    public static StateListDrawable createRoundButtonBackground(Context context, int color, float radius, boolean solid) {
        if (solid) {
            return createSolidRoundButtonBackground(color, radius);
        } else {
            return createRoundButtonBackground(context, color, radius);
        }
    }

    /**
     * 创建普通圆角按钮背景
     * @param radius    圆角半径
     */
    public static StateListDrawable createRoundButtonBackground(Context context, float radius) {
        StateListDrawable stateDrawable = new StateListDrawable();
        int strokeWidth = dip2px(context, DRAWABLE_STROKE_WIDTH);

        GradientDrawable normal = createGradientDrawable(Color.WHITE, DRAWABLE_DISABLE_COLOR, strokeWidth, radius);
        GradientDrawable pressed = createGradientDrawable(createPressedColor(Color.WHITE), DRAWABLE_DISABLE_COLOR, strokeWidth, radius);
        GradientDrawable disable = createGradientDrawable(Color.WHITE, DRAWABLE_DISABLE_COLOR, strokeWidth, radius);

        stateDrawable.addState(new int[]{-android.R.attr.state_enabled}, disable);
        stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateDrawable.addState(new int[]{}, normal);

        return stateDrawable;
    }

    /**
     * 创建按钮文字颜色
     * @param normalColor   普通状态
     * @param pressColor    按压状态
     * @param disableColor  不可用状态
     */
    public static ColorStateList createTextStateColor(int normalColor, int pressColor, int disableColor) {
        int[] colors = new int[]{disableColor, pressColor, normalColor};
        int[][] state = new int[3][];

        state[0] = new int[]{-android.R.attr.state_enabled};
        state[1] = new int[]{android.R.attr.state_pressed};
        state[2] = new int[]{};
        return new ColorStateList(state, colors);
    }

    private static StateListDrawable createSolidRoundButtonBackground(int color, float radius) {
        StateListDrawable stateDrawable = new StateListDrawable();
        GradientDrawable normal = createGradientDrawable(color, 0, 0, radius);
        GradientDrawable pressed = createGradientDrawable(createPressedColor(color), 0, 0, radius);
        GradientDrawable disable = createGradientDrawable(DRAWABLE_DISABLE_COLOR, 0, 0, radius);

        stateDrawable.addState(new int[]{-android.R.attr.state_enabled}, disable);
        stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateDrawable.addState(new int[]{}, normal);

        return stateDrawable;
    }

    private static StateListDrawable createRoundButtonBackground(Context context, int color, float radius) {
        StateListDrawable stateDrawable = new StateListDrawable();
        int strokeWidth = dip2px(context, DRAWABLE_STROKE_WIDTH);

        GradientDrawable normal = createGradientDrawable(Color.WHITE, color, strokeWidth, radius);
        GradientDrawable pressed = createGradientDrawable(color, 0, 0, radius);
        GradientDrawable disable = createGradientDrawable(Color.WHITE, DRAWABLE_DISABLE_COLOR, strokeWidth, radius);

        stateDrawable.addState(new int[]{-android.R.attr.state_enabled}, disable);
        stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateDrawable.addState(new int[]{}, normal);

        return stateDrawable;
    }

    public static GradientDrawable createGradientDrawable(int color, int strokeColor,
                                                           int strokeWidth, float radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(radius);
        drawable.setColor(color);
        drawable.setStroke(strokeWidth, strokeColor);

        return drawable;
    }

    /**
     * 创建带分割线的背景
     */
    public static LayerDrawable createItemSeparatorBg(@ColorInt int separatorColor, @ColorInt int bgColor, int separatorHeight, boolean top) {
        ShapeDrawable separator = new ShapeDrawable();
        separator.getPaint().setStyle(Paint.Style.FILL);
        separator.getPaint().setColor(separatorColor);

        ShapeDrawable bg = new ShapeDrawable();
        bg.getPaint().setStyle(Paint.Style.FILL);
        bg.getPaint().setColor(bgColor);

        Drawable[] layers = {separator, bg};
        LayerDrawable layerDrawable = new LayerDrawable(layers);

        layerDrawable.setLayerInset(1, 0, top ? separatorHeight : 0, 0, top ? 0 : separatorHeight);
        return layerDrawable;
    }

    /**
     * 创建带分割线的圆角drawable
     */
    public static LayerDrawable createSeparatorDrawable(int solidColor, int dividerColor,
                                                        int dividerSize, float[] radii) {
        GradientDrawable divider = new GradientDrawable();
        divider.setColor(dividerColor);
        divider.setCornerRadii(radii);

        GradientDrawable solid = new GradientDrawable();
        solid.setColor(solidColor);
        solid.setCornerRadii(radii);

        Drawable[] layers = {divider, solid};
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        layerDrawable.setLayerInset(1, 0, dividerSize, 0, 0);

        return layerDrawable;
    }
}
